package com.Jiang.servlet.User;

import com.Jiang.pojo.User;
import com.Jiang.service.User.UserService;
import com.Jiang.service.User.UserServiceImplements;
import com.Jiang.utillity.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

public class LoginServlet extends HttpServlet {
    private UserService userService;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    userService=new UserServiceImplements();
        //getParameter  获取参数
        //getAttribute 获取属性

        User user= null;
        try {
            user = userService.Login((String) req.getParameter("userCode"),(String)req.getParameter("userPassword"));

        //System.out.println(user.getUserRoleName());
        if(user!=null){ //成功,设置sessiong的值并重定向
            req.getSession().setAttribute(Constants.USER_SESSION,user);
            resp.sendRedirect(req.getContextPath()+"/jsp/frame.jsp");
        }
        else{   //失败,无法登陆,发送错误信息

        req.setAttribute("error","用户名或者密码错误!");
        req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
